

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
	qui do ".../replication_package/housekeeping.do"

	*Write log
	log using "$log_loc/timely_claim_rate.log", text replace

	*Create Empty Tempfile to Store Regression Results
		tempfile fig_1
		save `fig_1', emptyok

/*------------------------------------------------------------------------------
	4. Clean Main Datasets
------------------------------------------------------------------------------*/

	forvalues year = 2017/2021 {

		*Import Data
		use "$data/childrens`year'.dta", clear

			fmerge m:1 in_data using "$data/filed_flag_`year'.dta"
				drop if _merge == 2
				ren (_merge) (in_data_flag)
	
			/*Restrict Sample*/
				*Keep observations in the 51 states (per the f1095 variable). Note that most of the dropped state values are missing.
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if `year'*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12
	
			/*Generate variables for table*/
				*Generate outcome variable on claiming
					gen onreturn = file_inc != .  & flag_filed_in_yr1 == 1

				*Generate a ct variable
					gen ct = 1

				*Generate variable for year
					gen year = `year'
			
/*------------------------------------------------------------------------------
	6. Carry Out Estimated Calculations
------------------------------------------------------------------------------*/

			/*Prepare backup data*/
				*Collapse to the by-age_num level		
					collapse (sum) ct onreturn, by(year)
			
				*Adjust for Claiming Limits
					replace onreturn = onreturn * 1.0017029
					
				*Create Percent Claimed variables
					gen perc_claimed = onreturn/ct
					

				*Keep key outcomes
					keep perc_claimed year

		*Save results as a tempfile
			tempfile results_`year'
			save `results_`year'', replace
	}

/*------------------------------------------------------------------------------
	7. Export the results as an Excel File
------------------------------------------------------------------------------*/

	*Import and append the results from 2017 to 2021
	forvalues year = 2017/2021 {
		use `results_`year'', clear		
		append using `fig_1'

		tempfile fig_1
		save `fig_1', replace
	
	}
	
		*Organize before export
			sort year
			order year perc_claimed 

	*Export Results as an Excel File		
			sort year
			gen f_label = _n
			replace perc_claimed = perc_claimed * 100

			twoway (bar perc_claimed f_label, lcolor(black) lwidth(vthin) barwidth(.95) color("4 108 154"*85)), ///
				xlabel(1 "2017" 2 "2018" 3 "2019" 4 "2020" 5 "2021", labsize(small)) ///
				ylabel(90(1)95, format(%5.1f) labsize(small)) ///
				xtitle("Year", height(4)) ///
				ytitle("Share Claimed (%)", size(medsmall)) ///
				graphregion(fcolor(white) color(white) lcolor(white))
	
				*Export Graph
					graph save "$output/timely_child_claiming.gph", replace 
